class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        int left = 0;
        int right = -1;
        while (left < arr.size())
        {
            if (arr[left] != 0)
            {
                right++;
            }
            else
            {
                right += 2;
            }
            if (right >= arr.size() - 1) break;
            left++;
        }
        if (right == arr.size())
        {
            arr[right - 1] = 0;
            left--;
            right -= 2;
        }
        while (left >= 0 && right >= 0)
        {
            arr[right] = arr[left];
            right--;
            if (arr[left] == 0 && right >= 0)
            {
                arr[right] = arr[left];
                right--;
            }
            left--;
        }
    }
};